from fastapi import APIRouter, HTTPException
from ..service.student_service import StudentService
from ..model.student_model import StudentCreate, StudentUpdate, StudentResponse
from common.response_model import UnifiedResponse

router = APIRouter()
student_service = StudentService()


@router.post("/students", response_model=UnifiedResponse)
def create_student(student: StudentCreate):
    """创建新学生"""
    created_student = student_service.create(student)
    return UnifiedResponse.success(data=created_student)


@router.get("/students/{student_id}", response_model=UnifiedResponse)
def read_student(student_id: int):
    """获取单个学生"""
    student = student_service.read(student_id)
    if not student:
        raise HTTPException(status_code=404, detail="Student not found")
    return UnifiedResponse.success(data=student)


@router.get("/students", response_model=UnifiedResponse)
def read_all_students():
    """获取所有学生"""
    students = student_service.read_all()
    return UnifiedResponse.success(data=students)


@router.put("/students/{student_id}", response_model=UnifiedResponse)
def update_student(student_id: int, student: StudentUpdate):
    """更新学生信息"""
    updated_student = student_service.update(student_id, student)
    if not updated_student:
        raise HTTPException(status_code=404, detail="Student not found")
    return UnifiedResponse.success(data=updated_student)


@router.delete("/students/{student_id}", response_model=UnifiedResponse)
def delete_student(student_id: int):
    """删除学生"""
    if not student_service.delete(student_id):
        raise HTTPException(status_code=404, detail="Student not found")
    return UnifiedResponse.success(message="Student deleted successfully")
